import {BrowserModule} from '@angular/platform-browser';
import {NgModule} from '@angular/core';
import {FormsModule} from '@angular/forms';
import {HttpModule} from '@angular/http';
import {InterceptorService} from 'ng2-interceptors/index';
import {Http, XHRBackend, RequestOptions} from '@angular/http';
import {HashLocationStrategy, LocationStrategy} from '@angular/common';
import {AppRoutingModule} from './app-routing.module';
import {HttpInterceptor} from './common/http-interceptor.service';
import {BusinessModule} from './business.module';
import {AuthService} from './user/auth.service';
import {UserService} from './user/user.service';
import {AppComponent} from './app.component';
import {LoginComponent} from './user/login.component';

export function interceptorFactory(xhrBackend: XHRBackend,
                                   requestOptions: RequestOptions,
                                   httpInterceptor: HttpInterceptor) { // Add it here
  const service = new InterceptorService(xhrBackend, requestOptions);
  service.addInterceptor(httpInterceptor); // Add it here
  return service;
}

@NgModule({
  declarations: [
    AppComponent,
    LoginComponent
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule,
    BusinessModule,
    AppRoutingModule
  ],
  providers: [HttpInterceptor, UserService, AuthService, {provide: LocationStrategy, useClass: HashLocationStrategy}, {
    provide: Http,
    useFactory: interceptorFactory,
    deps: [XHRBackend, RequestOptions, HttpInterceptor]
  }],
  bootstrap: [AppComponent]
})
export class AppModule {
}
